﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Chuong02
{
    public class FreeCamera: Camera
    {
        private float yaw;

        public float Yaw
        {
            get { return yaw; }
            set { yaw = value; }
        }
        private float pitch;

        public float Pitch
        {
            get { return pitch; }
            set { pitch = value; }
        }
        private Vector3 position;

        public Vector3 Position
        {
            get { return position; }
            set { position = value; }
        }
        private Vector3 target;

        public Vector3 Target
        {
            get { return target; }
            set { target = value; }
        }
        private Vector3 translation; //Tính khoảng cách camara đã di chuyển

        public Vector3 Translation
        {
            get { return translation; }
            set { translation = value; }
        }

        public FreeCamera(Vector3 position , float yaw, float Pitch,GraphicsDevice  graphicsDevice) : base (graphicsDevice)
        {
            this.Yaw = yaw;    //Camera di chuyen truc Y bao nhieu. Tinh theo radian
            this.Pitch = pitch;  //Camera di chuyen truc X bao nhieu Tinh theo radian
            //Duoc tinh thong qua ham Rotate.
            translation = Vector3.Zero;
        }
         //Cập nhật lại giá trị Yaw và Pitch
        public void Rotate(float yawChange, float pitchChange)
        {
            this.Yaw = yawChange;
            this.Pitch = pitchChange;
        }

        //Tính khoảng cách mà camara đã di chuyển

        public void Move(Vector3 Translation)
        {
            this.translation = Translation;
        }

          //Tính toán ma trận View mới 
        public override void Update()
        {
            Matrix _Rotation = Matrix.CreateFromYawPitchRoll(Yaw, Pitch, 0); //Camare xoay quanh trục x và y

            this.translation = Vector3.Transform(translation, _Rotation);
            this.Position += translation;    //Di chuyển camera;

            this.translation = Vector3.Zero;

              //Tính toán điểm mới :
            Vector3 forward = Vector3.Transform(Vector3.Forward, _Rotation);
            
            this.Target = Position + forward;
            
            Vector3 up = Vector3.Transform(Vector3.Up, _Rotation);

            this.View = Matrix.CreateLookAt(Position, Target, up);
            base.Update();
        }

        
    }

}
